<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Services::Middleware::AMQPLogWriter</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Services::Middleware::AMQPLogWriter
                <span class="parent">&lt;
                    <a href="../../Logger/FastWriter.html">Hoodoo::Logger::FastWriter</a>
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../../files/lib/hoodoo/services/middleware/amqp_log_writer_rb.html">lib/hoodoo/services/middleware/amqp_log_writer.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>Log writer which sends structured messages to an AMQP-based queue via the
Alchemy Flux gem. A <a
href="../../Logger/FastWriter.html">Hoodoo::Logger::FastWriter</a>
subclass, since though talking to the queue might be comparatively slow,
Flux uses Event Machine for this so there&#39;s no need to add another
Thread for this logger.</p>

<p>See also <a href="../../Logger.html">Hoodoo::Logger</a>.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>N</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-new">new</a>
              </li>
          </ul>
        </dd>
        <dt>R</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-report">report</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-new">
              <b>new</b>( alchemy, routing_key = nil )
            <a href="../../../../classes/Hoodoo/Services/Middleware/AMQPLogWriter.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Create an AMQP logger instance.</p>
<dl class="rdoc-list note-list"><dt>alchemy
<dd>
<p>The Alchemy endpoint to use for sending messages to the AMQP-based queue.</p>
</dd><dt><code>routing_key</code>
<dd>
<p>The routing key (as a String) to use. Optional. If omitted, reads
<code>ENV['AMQ_LOGGING_ENDPOINT']</code> or if that is unset, defaults to
<code>platform.logging</code>.</p>
</dd></dl>

<p>If you&#39;re running with <a href="../../../Rack.html">Rack</a> on top of
Alchemy, then the <code>call</code> method&#39;s <code>env</code> parameter
containing the <a href="../../../Rack.html">Rack</a> environment
<em>MUST</em> have a key of <code>alchemy.service</code> with a value
that&#39;s the AlchemyFlux::Service instance handling queue communication.
This is assigned to the <code>alchemy</code> parameter. The logger will
then use this active Alchemy service to send messages to its configured
routing key.</p>

<p>If <code>ENV['AMQ_ANALYTICS_LOGGING_ENDPOINT']</code> is defined then its
value is used for a routing key in the case, very specifically, of a
message logged with a <code>code</code> of <code>analytics</code>. If the
variable is not set, the same routing key is used for all messages
regardless of code; else that particular code can be streamed off to
another Rabbit queue via the given alternative routing key.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/middleware/amqp_log_writer.rb, line 47</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>( <span class="ruby-identifier">alchemy</span>, <span class="ruby-identifier">routing_key</span> = <span class="ruby-keyword">nil</span> )
  <span class="ruby-identifier">routing_key</span>           = <span class="ruby-identifier">routing_key</span> <span class="ruby-operator">||</span> <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;AMQ_LOGGING_ENDPOINT&#39;</span> ] <span class="ruby-operator">||</span> <span class="ruby-string">&#39;platform.logging&#39;</span>
  <span class="ruby-identifier">analytics_routing_key</span> = <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;AMQ_ANALYTICS_LOGGING_ENDPOINT&#39;</span> ]

  <span class="ruby-ivar">@alchemy</span>      = <span class="ruby-identifier">alchemy</span>
  <span class="ruby-ivar">@routing_keys</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">routing_key</span> ) <span class="ruby-comment"># Use &quot;routing_key&quot; as a default value</span>

  <span class="ruby-ivar">@routing_keys</span>[ <span class="ruby-value">:analytics</span> ] = <span class="ruby-identifier">analytics_routing_key</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">routing_key</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-report">
              <b>report</b>( level, component, code, data )
            <a href="../../../../classes/Hoodoo/Services/Middleware/AMQPLogWriter.html#method-i-report" name="method-i-report" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Custom implementation of the <a
href="../../Logger/WriterMixin.html#method-i-report">Hoodoo::Logger::WriterMixin#report</a>
interface. See that method for parameter details.</p>

<p>The middleware custom logger has expectations about the data payload. It
expects these optional but recommended (where the information is available
/ has been generated) keys/values:</p>
<dl class="rdoc-list note-list"><dt><code>:id</code>
<dd>
<p>A <a href="../../UUID.html">UUID</a> (via <a
href="../../UUID.html#method-c-generate">Hoodoo::UUID.generate</a>) to use
for this log message - if absent, one is generated automatically.</p>
</dd><dt><code>:session</code>
<dd>
<p>Description of the current request session when available; a <a
href="../Session.html">Hoodoo::Services::Session</a> as a Hash (via to_h;
keys as Strings). The Caller <a href="../../UUID.html">UUID</a>, identity
Participant <a href="../../UUID.html">UUID</a> and identity Outlet <a
href="../../UUID.html">UUID</a> are sent as independent, searchable fields
in the log payload.</p>
</dd><dt><code>interaction_id</code>
<dd>
<p>The interaction ID for this client&#39;s call. This is also sent as an
independent, searchable field in the log payload.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-report_source')" id="l_method-i-report_source">show</a>
              </p>
              <div id="method-i-report_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/middleware/amqp_log_writer.rb, line 77</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">report</span>( <span class="ruby-identifier">level</span>, <span class="ruby-identifier">component</span>, <span class="ruby-identifier">code</span>, <span class="ruby-identifier">data</span> )
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@alchemy</span>.<span class="ruby-identifier">nil?</span>

  <span class="ruby-comment"># Take care with Symbol keys in &#39;data&#39; vs string keys in e.g. &#39;session&#39;.</span>

  <span class="ruby-identifier">session</span> = <span class="ruby-identifier">data</span>[ <span class="ruby-value">:session</span> ] <span class="ruby-operator">||</span> {}
  <span class="ruby-identifier">message</span> = {
    <span class="ruby-value">:id</span>                   =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>[ <span class="ruby-value">:id</span> ],
    <span class="ruby-value">:level</span>                =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">level</span>,
    <span class="ruby-value">:component</span>            =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">component</span>,
    <span class="ruby-value">:code</span>                 =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">code</span>,
    <span class="ruby-value">:reported_at</span>          =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">iso8601</span>( <span class="ruby-number">12</span> ),

    <span class="ruby-value">:interaction_id</span>       =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>[ <span class="ruby-value">:interaction_id</span> ],
    <span class="ruby-value">:data</span>                 =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">data</span>,

    <span class="ruby-value">:caller_id</span>            =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">session</span>[ <span class="ruby-string">&#39;caller_id&#39;</span>            ],
    <span class="ruby-value">:caller_identity_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">session</span>[ <span class="ruby-string">&#39;caller_identity_name&#39;</span> ],
    <span class="ruby-value">:identity</span>             =<span class="ruby-operator">&gt;</span> ( <span class="ruby-identifier">session</span>[ <span class="ruby-string">&#39;identity&#39;</span> ] <span class="ruby-operator">||</span> {} ).<span class="ruby-identifier">to_h</span>
  }.<span class="ruby-identifier">to_json</span>()

  <span class="ruby-ivar">@alchemy</span>.<span class="ruby-identifier">send_message_to_service</span>(
    <span class="ruby-ivar">@routing_keys</span>[ <span class="ruby-identifier">code</span>.<span class="ruby-identifier">to_sym</span> ],
    { <span class="ruby-string">&quot;body&quot;</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">message</span> }
  )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
